import request from '@/utils/request'
import 'core-js/stable'

export const classApi = {
  // 获取班级详情
  getClassDetail(classId) {
    return request({
      url: `/api/spoc/cla/full-detail/${classId}`,
      method: 'get'
    }).then(response => ({
      data: response.data || response
    })).catch(error => {
      console.error('获取班级详情失败:', error)
      throw error
    })
  },

  // 获取班级学生
  getClassStudents(classId) {
    return request({
      url: `/api/spoc/cla/${classId}/students/basic`,
      method: 'get'
    }).then(response => ({
      data: response.data || response
    })).catch(error => {
      console.error('获取班级学生失败:', error)
      throw error
    })
  },

  // 获取班级教师
  getClassTeachers(classId) {
    return request({
      url: `/api/spoc/cla/${classId}/teachers`,
      method: 'get'
    }).then(response => ({
      data: response.data || response
    })).catch(error => {
      console.error('获取班级教师失败:', error)
      throw error
    })
  },

  // 获取班级课程
  getClassCourses(classId) {
    return request({
      url: `/api/spoc/cla/${classId}/courses`,
      method: 'get'
    }).then(response => ({
      data: response.data || response
    })).catch(error => {
      console.error('获取班级课程失败:', error)
      throw error
    })
  },

  // 移除学生
  removeStudentFromClass(classId, studentId) {
    return request({
      url: `/api/spoc/cla/${classId}/students/${studentId}`,
      method: 'delete'
    }).catch(error => {
      console.error('移除学生失败:', error)
      throw error
    })
  },

  // 移除教师
  removeTeacherFromClass(classId, teacherId) {
    return request({
      url: `/api/spoc/cla/${classId}/teachers/${teacherId}`,
      method: 'delete'
    }).catch(error => {
      console.error('移除教师失败:', error)
      throw error
    })
  },

  // 获取班级列表
  getClassList() {
    return request.get('/api/spoc/cla/listAll')
  },

  // 更新班级信息
  updateClassInfo(data) {
    return request({
      url: `/api/spoc/cla/${data.id}`,
      method: 'put',
      data: {
        className: data.className,
        classStuNum: data.classStuNum
      }
    })
  },

  // 批量创建班级
  batchCreateClasses(classes) {
    return request.post('/api/spoc/cla/batchCreate', classes)
  },

  // 删除班级
  deleteClass(id) {
    return request.delete(`/api/spoc/cla/${id}`)
  },

  // 获取嵌套树形列表
  getNestedTreeList(teacherId) {
    return request.get(`/vod/chapter/getNestedTreeListByTeacher/${teacherId}`)
  },

  // 章节相关操作
  saveChapter(data) {
    return request.post('/vod/chapter/save', data)
  },

  updateChapter(data) {
    return request.post('/vod/chapter/update', data)
  },

  deleteChapter(id) {
    return request.delete(`/vod/chapter/remove/${id}`)
  },

  updateChapterIndex(data) {
    return request.post('/vod/chapter/updateIndex', data)
  },

  getChapter(id) {
    return request.get(`/vod/chapter/get/${id}`)
  },

  // 检查教师班级权限
  checkTeacherClass(teacherId, classId) {
    return request({
      url: `/spoc/teacher/teacher/${teacherId}/classes/${classId}`,
      method: 'get'
    })
  },

  // 添加课程到班级
  addCourseToClass(classId, courseId) {
    return request({
      url: `/spoc/vod/course/${classId}/courses/${courseId}`,
      method: 'post'
    })
  },

  // 批量添加课程到班级
  batchAddCoursesToClass(classId, courseIds) {
    return request({
      url: `/spoc/vod/course/${classId}/courses/batch`,
      method: 'post',
      data: courseIds
    })
  },

  // 从班级移除课程
  removeCourseFromClass(classId, courseId) {
    return request({
      url: `/spoc/vod/course/${classId}/courses/${courseId}`,
      method: 'delete'
    })
  }
}